package
{
	import flash.events.MouseEvent;

	public class InputManager
	{
		public var vrend:VectorRenderer;
		public var mPos:Vector2;
		public var mOldpos:Vector2;
		public var mDelta:Vector2;
		public var mDownPos:Vector2;
		public var mUpPos:Vector2;
		public var onMouseDown:Function;
		public var onMouseUp:Function;
		public var mState:Boolean;
		public var mOldState:Boolean;
		public var mPressed:Boolean;
		public var mReleased:Boolean;
		public var kCode:Array;
		public var kState:Array;
		public var kOldState:Array;
		public var tKey:Array;
		public var tState:Array;

		public function InputManager()
		{
			this.vrend = new VectorRenderer();
			this.mPos = new Vector2(this.vrend.buffer.mouseX, this.vrend.buffer.mouseY);
			this.mOldpos = new Vector2(this.vrend.buffer.mouseX, this.vrend.buffer.mouseY);
			this.mDelta = new Vector2(0, 0);
			this.mDownPos = new Vector2(0, 0);
			this.mUpPos = new Vector2(0, 0);

			this.onMouseDown = this.CaptureMouseDown;
			this.onMouseUp = this.CaptureMouseUp;
			Main.getInstance().stage.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
			Main.getInstance().stage.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);

			this.mState = false;
			this.mOldState = false;
			this.mPressed = false;
			this.mReleased = false;
			this.kCode = new Array();
			this.kState = new Array();
			this.kOldState = new Array();
			this.tKey = new Array();
			this.tState = new Array();
		}

		public function RegisterKey(knum):Number
		{
			var _local1 = this.kCode.length;
			this.kCode.push(knum);
			this.kState[_local1] = false;
			this.kOldState[_local1] = false;
			return(_local1);
		}

		public function RegisterToggle(knum):Number
		{
			var _local2 = this.tKey.length;
			this.tKey.push(this.RegisterKey(knum));
			this.tState[_local2] = false;
			return(_local2);
		}

		public function Update():void
		{
			this.mOldpos.x = this.mPos.x;
			this.mOldpos.y = this.mPos.y;
			this.mPos.x = this.vrend.buffer.mouseX;
			this.mPos.y = this.vrend.buffer.mouseY;
			this.mDelta.x = this.mPos.x - this.mOldpos.x;
			this.mDelta.y = this.mPos.y - this.mOldpos.y;
			if (this.mState && (!this.mOldState))
			{
				this.mPressed = true;
				this.mOldState = true;
				this.mDownPos.x = this.mPos.x;
				this.mDownPos.y = this.mPos.y;
			}
			else
			{
				this.mPressed = false;
			}
			if ((!this.mState) && (this.mOldState))
			{
				this.mReleased = true;
				this.mOldState = false;
				this.mUpPos.x = this.mPos.x;
				this.mUpPos.y = this.mPos.y;
			}
			else
			{
				this.mReleased = false;
			}
			if (this.mState)
			{
				this.mUpPos.x = this.mPos.x;
				this.mUpPos.y = this.mPos.y;
			}
			var _local2 = 0;
			while (_local2 < this.kCode.length)
			{
				this.kOldState[_local2] = KeyManager.isDown(this.kCode[_local2]);
				_local2++;
			}
			var _local3 = this.kOldState;
			this.kOldState = this.kState;
			this.kState = _local3;
			_local2 = 0;
			while (_local2 < this.tKey.length)
			{
				if (this.Pressed(this.tKey[_local2]))
				{
					this.tState[_local2] = !this.tState[_local2];
				}
				_local2++;
			}
		}

		private function CaptureMouseDown(event:MouseEvent):void
		{
			this.mOldState = false;
			this.mState = true;
		}

		private function CaptureMouseUp(event:MouseEvent):void
		{
			this.mOldState = true;
			this.mState = false;
		}

		public function getMousePos():Vector2
		{
			return(this.mPos.clone());
		}

		public function getMouseDelta():Vector2
		{
			return(this.mDelta.clone());
		}

		public function getMouseDragDelta():Vector2
		{
			return(this.mUpPos.minus(this.mDownPos));
		}

		public function getMouseDownPos():Vector2
		{
			return(this.mDownPos.clone());
		}

		public function getMouseUpPos():Vector2
		{
			return(this.mUpPos.clone());
		}

		public function MousePressed():Boolean
		{
			return this.mPressed;
		}

		public function MouseReleased():Boolean
		{
			return this.mReleased;
		}

		public function MouseDown():Boolean
		{
			return this.mState;
		}

		public function Down(knum)
		{
			return(this.kState[knum]);
		}

		public function Pressed(knum)
		{
			return(this.kState[knum] && (!this.kOldState[knum]));
		}

		public function Released(knum)
		{
			return((!this.kState[knum]) && (this.kOldState[knum]));
		}

		public function Toggled(tnum)
		{
			return(this.tState[tnum]);
		}
	}
}